#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [Gtlions Lai].
# Create Date:
# Update Date:
__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'
"""Function for GreenPlum DB tables partitions manager - add partitions.

Add tables partitions.

  ClassFoo: None.
  functionBar(): None.
"""

import sys
import os
import platform
import colorama
import time

if platform.system() == 'Windows' or platform.system() == 'Darwin':
    from psycopg2 import *
elif platform.system() == 'Linux':
    from pygresql.pgdb import *
import datetime
import dsn
import partitioncom

db = connect(host=dsn.host, database=dsn.database, user=dsn.user, password=dsn.password)
cur = db.cursor()
begin_time = datetime.datetime.today()
cur.execute("select datname from pg_database order by datname")
for ret in cur.fetchall():
    cur.execute(
        "insert into gpmg.dbsize(rq,datname,size,size_h) select %s,%s,pg_database_size(%s),pg_size_pretty(pg_database_size(%s))",
        (begin_time, ret[0], ret[0], ret[0]))
cur.execute(
    "insert into gpmg.dbsize(rq,datname,size,size_h) select  rq,'TotalSize:',sum(size),pg_size_pretty(sum(size)::bigint) from gpmg.dbsize where rq=%s group by rq",
    (begin_time,))
cur.close()
db.commit()
db.close()
